
package com.scalar.chiptrack.operations.process.actions;


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.FabDAO;
import com.scalar.chiptrack.operations.process.dao.FabStage;
import com.scalar.chiptrack.operations.process.dao.FabSubStage;
import com.scalar.chiptrack.operations.process.forms.FabStageForm;






public final class FabStageAction extends ChipTrackAction {

    public FabStageAction() {
    }

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {

			 HttpSession ses = request.getSession(true);


			FabStageForm fabStageForm = (FabStageForm) form;

			 int []  stageId			= fabStageForm.getStageId(); 	
			 String []  fabStepName		= fabStageForm.getFabStageName();
			 //int []  procedureDoc		= fabStageForm.getProcedureDoc();
			 String []  comments		= fabStageForm.getComments();
			 int []  order				= fabStageForm.getOrder();
			 String [] type				= fabStageForm.getType();

			 boolean success = false;
			 
			 
			 Integer processStepId =(Integer)ses.getAttribute("processStepId");
			 String productId  = (String)ses.getAttribute("productId");
			 //System.out.println("Product ID in Fab stage = "+ productId);
			 int fabStageId=0;
			 ArrayList fabStageList = new ArrayList();
			 ArrayList fabSubStageList = new ArrayList();
			 FabStage fabStage = null;
			 FabSubStage fabSubStage = null;
			 FabDAO fabDAO = new FabDAO();

			DBTransaction dbTransaction = null;
			

			try{


			try
				{
				dbTransaction = DBTransaction.getInstance();
				dbTransaction.begin();		
				}
				catch (Throwable t)
				{
					t.printStackTrace();
				}

				 
			
			for(int i=0;i<fabStepName.length;i++)
			{
				if(type[i].equals("new") && !fabStepName[i].equals("")){
				
				fabStage = new FabStage();				
				fabStage.setFabStageName(fabStepName[i]);
				//fabStage.setProcedureDoc(procedureDoc[i]);
				fabStage.setOrder(i+1);
				fabStage.setComments(comments[i]);
				
				fabStageId = FabDAO.saveFabStage(dbTransaction,processStepId.intValue(),fabStage);
				String strStageId = new Integer(stageId[i]).toString();
				fabSubStageList=(ArrayList)ses.getAttribute(strStageId);
				if(fabSubStageList!=null)
					FabDAO.saveFabSubStage(dbTransaction,fabStageId,fabSubStageList.iterator());
				ses.removeAttribute(strStageId);

				}
				
			}	 

				request.setAttribute("baseProductID", productId);
				success	= true;
			}

		catch(Exception e){ e.printStackTrace();}

		finally{

			if (success == true) {
                    dbTransaction.commit();
					//System.out.println("debug7");	
                }
                else {
                    dbTransaction.rollback();
                }

		}
			
			 return (mapping.findForward("success")); 
	}
	
}